﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DSheet.Core;
using DSheet.Core.Data;

namespace DExcelServiceApp
{
    public partial class DimDataList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (DBContext context = new DBContext())
                {
                    List<DimData> datas = new DimDataService().GetAllDims(context);
                    _ctlGridDimData.DataSource = datas;
                    _ctlGridDimData.DataBind();
                }
            }
        }

        protected void _ctlBtnCreateDimData_Click(object sender, EventArgs e)
        {
            DimData dimdata = new DimData();
            dimdata.Id = Guid.NewGuid();
            dimdata.Code = _ctlEditCode.Text.Trim();
            dimdata.Name = _ctlEditName.Text.Trim();

            using (DBContext context = new DBContext())
            {
                new DimDataService().SaveDimData(context, dimdata);

                List<DimData> datas = new DimDataService().GetAllDims(context);
                _ctlGridDimData.DataSource = datas;
                _ctlGridDimData.DataBind();
            }
        }

        protected void _ctlGridDimData_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataKey datas = _ctlGridDimData.SelectedDataKey;
            //DimData selData = datas[_ctlGridDimData.SelectedIndex];
            using (DBContext context = new DBContext())
            {
                _ctlHiddenDataCode.Value = datas.Value.ToString();

                DimData dimData = new DimDataService().GetDimData(context, new Guid(datas.Value.ToString()));
                List<DimRowItem> dimRows = dimData.Complanate();
                _ctlGridItems.DataSource = dimRows;
                _ctlGridItems.DataBind();
            }
        }

        protected void _ctlBtnCreateItem_Click(object sender, EventArgs e)
        {
            DimItem dimItem = new DimItem();
            dimItem.Id = Guid.NewGuid();
            dimItem.Code = string.Format("{0}{1}", _ctlHiddenParentCode.Value, _ctlEditItemCode.Text.Trim());
            dimItem.Name = _ctlEditItemName.Text.Trim();

            using (DBContext context = new DBContext())
            {
                if (string.IsNullOrEmpty(_ctlHiddenParentCode.Value))
                {
                    new DimDataService().SaveDimItem(context, new Guid(_ctlHiddenDataCode.Value), Guid.Empty, dimItem);
                }
                else
                {
                    new DimDataService().SaveDimItem(context, new Guid(_ctlHiddenDataCode.Value), new Guid(_ctlHiddenParentId.Value), dimItem);
                }

                DimData dimData = new DimDataService().GetDimData(context, new Guid(_ctlHiddenDataCode.Value));
                List<DimRowItem> dimRows = dimData.Complanate();
                _ctlGridItems.DataSource = dimRows;
                _ctlGridItems.DataBind();
            }
        }

        protected void _ctlGridItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DBContext context = new DBContext())
            {
                DimItem item = new DimDataService().GetDimItem(
                    context,
                    new Guid(_ctlHiddenDataCode.Value),
                    _ctlGridItems.SelectedValue.ToString());
                this._ctlHiddenParentId.Value = item.Id.ToString();
                this._ctlHiddenParentCode.Value = item.Code;
                _ctlEditItemPCode.Text = item.Name;
            }
        }

        protected void _ctlBtnClear_Click(object sender, EventArgs e)
        {
            this._ctlHiddenParentCode.Value = string.Empty;
            _ctlEditItemPCode.Text = string.Empty;
        }
    }
}